package com.hhhn.wk.utils;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.entity.User;
import com.hhhn.wk.h5.LinkH5Activity;
import com.hhhn.wk.login.LoginActivity;
import com.hhhn.wk.main.tab2.view.CommodityListActivity;
import com.hhhn.wk.utils.mi.AES;
import com.hhhn.wk.utils.mi.AESUtils;
import com.hhhn.wk.utils.mi.RSAUtils;
import com.hhhn.wk.wxapi.Constants_WX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import java.text.DecimalFormat;
import static com.hhhn.wk.main.fragment.TabFragment02.Classify_allTypeFragment1.TAG;
public class AllPublic {
    /*检查是否安装微信*/
    public static boolean isWXAppInstalledAndSupported(Context context) {
        IWXAPI msgApi = WXAPIFactory.createWXAPI(context, null);
        msgApi.registerApp(Constants_WX.APP_ID);
        boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled()
                && msgApi.isWXAppSupportAPI();
        return sIsWXAppInstalledAndSupported;
    }
    public static String priceGSH(String string){
        try {
            double v = Double.parseDouble(string);
            DecimalFormat df   = new DecimalFormat("######0.00");
            String format = df.format(v);
            return format;
        } catch (ArithmeticException e) {
            e.printStackTrace();
            return string;
        }
    }
    /**
     * 图片解析
     */
    public static void showImg(Context context, String url, ImageView imageView){
        Glide.with(context).load(Constants.HTTPIMG+url).into(imageView);
    }


    /**
     * 判断是否为货牛商户
     *  0未登录 未授权
     *  1 未登录 已授权
     *  2 已登录  已授权
     * 直接去登录
     */
    public static boolean isHuoNiu( BaseActivity activity){
        User user = (User) SharePreferencesUser.getBean(activity,Constants.USER);
        Log.d("user", user+"");
        if (user==null){
            return false;
        }
        int isLogin = user.getIsLogin();
        if (isLogin == 0) {
            return false;
        } else if (isLogin == 2) {//正常
            return true;//走自己的事情
        } else {
            return false;
        }
    }


    /**
     * 判断是否登录  islogin  1登录
     *  0未登录 未授权
     *  1 未登录 已授权
     *  2 已登录  已授权
     * 直接去登录
     */
    public static boolean isLogin(Context context , BaseActivity activity){
        User user = (User) SharePreferencesUser.getBean(activity,Constants.USER);
        Log.d("user", user+"");
        if (user==null){
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            return false;
        }
        int isLogin = user.getIsLogin();
        if (isLogin == 0) {
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            return false;
        } else if (isLogin == 2) {//正常
            return true;//走自己的事情
        } else {
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            return false;
        }
    }
    /**
     * H5
     * 跳转
     */
    public static void linkH5(Context context,BaseActivity activity, String title, String url){
        Intent intent=new Intent(context, LinkH5Activity.class);
        intent.putExtra("title",title);
        intent.putExtra("url",url);
        Log.d(TAG, "linkH5: "+url);
        activity.startActivity(intent);
    }
    /**
     * 接口请求头
     * 【收货地址】
     *
     */
    public static RequestParams getParams(BaseActivity activity, String string, boolean isRun){
        String head="shipAddress";
        String url=Constants.HTTPHHHN +head+string;
        RequestParams params = new RequestParams(url);
        //请求参数
//        String timestamp = "" + System.currentTimeMillis();
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
//        params.addParameter("app_type", Constants.TYPE);
//        params.addParameter("app_id", Constants.APP_ID);
//        params.addParameter("app_timestamp", timestamp);
//        if (isRun){
//            params.addParameter("app_signUid",activity.getUser().getUid());
//            Log.d("加密之前", "APP_ID: "+Constants.APP_ID);
//            Log.d("加密之前", "timestamp: "+timestamp);
//            Log.d("加密之前", "TYPE: "+Constants.TYPE);
//            Log.d("加密之前", "APP_SECRET: "+ Constants.APP_SECRET);
//            Log.d("加密之前", "Salt: "+activity.getUser().getSalt());
//            params.addParameter("app_sign", MD5Util.md5(Constants.APP_ID+ timestamp +Constants.TYPE   + Constants.APP_SECRET+activity.getUser().getSalt()));
//        }else {
//            params.addParameter("app_sign", MD5Util.md5(Constants.APP_ID+ timestamp +Constants.TYPE  + Constants.APP_SECRET));
//        }
        return params;
    }
    /**
     * 货牛
     * 接口请求头:Account
     * 【用户接口】
     */
    public static RequestParams getParamsAccount(BaseActivity activity, String string, boolean isRun){
        String head="account";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:Product
     * 【商品接口】
     */
    public static RequestParams getParamsProduct(BaseActivity activity, String string, boolean isRun){
        String head="product";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:type
     * 【分类接口】
     */
    public static RequestParams getParamsType(BaseActivity activity, String string, boolean isRun){
        String head="type";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:banner
     * 【banner接口】
     */
    public static RequestParams getParamsBanner(BaseActivity activity, String string, boolean isRun){
        String head="banner";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:search
     * 【搜索接口】
     */
    public static RequestParams getParamsSearch(BaseActivity activity, String string, boolean isRun){
        String head="search";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:cart
     * 【搜索接口】
     */
    public static RequestParams getParamsCart(BaseActivity activity, String string, boolean isRun){
        String head="cart";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
    * 货牛
     * 接口请求头:alipay
     * 【支付接口-支付宝】
     */
    public static RequestParams getParamsPay(BaseActivity activity, String string, boolean isRun){
        String head="alipay";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:weChat
     * 【支付接口-微信】
     */
    public static RequestParams getParamsPayWX(BaseActivity activity, String string, boolean isRun){
        String head="weChat";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:balance
     * 【支付接口-余额支付】
     */
    public static RequestParams getParamsPayBalance(BaseActivity activity, String string, boolean isRun){
        String head="balance";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:recharge
     * 【支付接口-余额充值】
     */
    public static RequestParams getParamsPayRecharge(BaseActivity activity, String string, boolean isRun){
        String head="recharge";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
    /**
    * 货牛
     * 接口请求头:productOrder
     * 【订单接口】
     */
    public static RequestParams getParamsProductOrder(BaseActivity activity, String string, boolean isRun){
        String head="productOrder";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        return params;
    }
    /**
    * 货牛
     * 接口请求头:valid
     * 【验证码】
     */
    public static RequestParams getParamsValid(BaseActivity activity, String string, boolean isRun){
        String head="valid";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        return params;
    }
    /**
    * 货牛
     * 接口请求头:shipAddress
     * 【地址】
     */
    public static RequestParams getParamsShipAddress(BaseActivity activity, String string, boolean isRun){
        String head="shipAddress";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        return params;
    }
    /**
    * 货牛
     * 接口请求头:version
     * 【版本】
     */
    public static RequestParams getParamsVersion(BaseActivity activity, String string, boolean isRun){
        String head="version";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        return params;
    }
       /**
    * 货牛
     * 接口请求头:dynamic
     * 【版本】
     */
    public static RequestParams getParamsDynamic(BaseActivity activity, String string, boolean isRun){
        String head="dynamic";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        return params;
    }
    /**
     * 货牛
     * 接口请求头:evaluate
     * 【评价】
     */
    public static RequestParams getParamsEvaluate(BaseActivity activity, String string, boolean isRun){
        String head="evaluate";
        String url=Constants.HTTPHHHN+head+string;
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type", "application/json");
        return params;
    }

    /**
     * 封装DES + 加密
     * @
     * @return
     */
    public static AES jsonDesPackaging(JSONObject person) {
        AES aes=null;
//        Log.e(TAG, "JOSN封装:"+person);
        Log.e(TAG, "JOSN封装:"+person.toString());
        /**
         * 前端,andriod,h5,ios
         * 1 首先是AES加密原始内容
         */
        String key = AESUtils.generateKey();
//        System.out.println("生成的随机字符串====" + key);
        try {
            aes=new AES();
            String data = AESUtils.encryptData(key, person.toString());//
            System.out.println("AES加密后的内容=====" + data);
            aes.setData(data);
            /**
             * 2.加密完原文后,对于生成的AES的Key值进行RSA 公钥加密
             */
            String rsaEncrypt_AesKey = RSAUtils.encryptByPublicKey(key);
            System.out.println("加密后的AES密钥====" + rsaEncrypt_AesKey);
            aes.setKey(rsaEncrypt_AesKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aes;
    }

    /**
     * 封装DES + 加密
     * @param
     * @return
     */
    public static AES jsonDesPackagF(com.alibaba.fastjson.JSONObject person) {
        AES aes=null;
//        Log.e(TAG, "JOSN封装:"+person);
        Log.e(TAG, "JOSN封装:"+person.toString());
        /**
         * 前端,andriod,h5,ios
         * 1 首先是AES加密原始内容
         */
        String key = AESUtils.generateKey();
//        System.out.println("生成的随机字符串====" + key);
        try {
            aes=new AES();
            String data = AESUtils.encryptData(key, person.toString());//
            System.out.println("AES加密后的内容=====" + data);
            aes.setData(data);
            /**
             * 2.加密完原文后,对于生成的AES的Key值进行RSA 公钥加密
             */
            String rsaEncrypt_AesKey = RSAUtils.encryptByPublicKey(key);
            System.out.println("加密后的AES密钥====" + rsaEncrypt_AesKey);
            aes.setKey(rsaEncrypt_AesKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aes;
    }



    /**
     * DES解密  廢棄
     * @param string
     * @return
     */
    public static String jsonDesDecode(String string){
//        if (DesUtils.isDES){
//            Log.e(TAG, "请求成功: "+string);
//            DesUtils desObj = new DesUtils();
//            string = desObj.strDec(string,DesUtils.key1, DesUtils.key2, DesUtils.key3);//解密
            Log.e(TAG, "解密成功: "+string);
//        }
        return string;
    }
//    /**
//     * 封装DES + 加密
//     * @param
//     * @return
//     */
//    public static String jsonDesPackaging(JSONObject person) {
//            String string="";
//            Log.e(TAG, "JOSN封装:"+person);
//            Log.e(TAG, "JOSN封装:"+person.toString());
//            DesUtils desObj = new DesUtils();
//            string = desObj.strEnc(person.toString(),DesUtils.key1, DesUtils.key2, DesUtils.key3);//加密
//            Log.e(TAG, "JOSN封装--加密:"+string);
////            String dec = desObj.strDec(string,DesUtils.key1, DesUtils.key2, DesUtils.key3);//解密
////            Log.e(TAG, "JOSN封装--加密--解密:"+dec);
//            return string;
//    }
//    /**
//     * DES解密
//     * @param string
//     * @return
//     */
//    public static String jsonDesDecode(String string){
//        if (DesUtils.isDES){
//            Log.e(TAG, "请求成功: "+string);
//            DesUtils desObj = new DesUtils();
//            string = desObj.strDec(string,DesUtils.key1, DesUtils.key2, DesUtils.key3);//解密
//            Log.e(TAG, "解密成功: "+string);
//        }
//        return string;
//    }
    /**
     * banner图跳转
     */
    public static void bannerURL(Context context, BaseActivity activity,String title,String web, String app){
        if (web!=null&&!"".equals(web)){
            try {
                AllPublic.linkH5(context, activity,title,web);
            } catch (Exception ignored) {
                activity.toastWk( ignored + "");
            }
        }else if (app!=null&&!"".equals(app)){
            //跳转到 二级分类 列表页面
            Intent intent = new Intent(activity, CommodityListActivity.class);
            intent.putExtra("title_str",title);
            intent.putExtra("categoryTwo",app);
            activity.startActivity(intent);
        }else {

        }
    }
} 